﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ResourceLoadBalancer.WebServices.Contracts.Data;
using ResourceLoadBalancer.WebServices.Clients.Helpers;
using System.Net;

namespace FileDuplicateFinderHasher
{
    public class FileHasher
    {
        IList<ResourceDataContract> resources = null;
        IList<ResourceURIDataContract> resourcesURI = null;
        protected ResourceLoadBalancerServiceClientHelper serviceClient = null;
        protected string hostName = null;
        protected string queueName = null;

        public FileHasher()
        {
            resources = new List<ResourceDataContract>();
            resourcesURI = new List<ResourceURIDataContract>();
            serviceClient = new ResourceLoadBalancerServiceClientHelper();
            hostName = Dns.GetHostName();
        }

        public IList<ResourceDataContract> Resources
        {
            get { return resources; }
            set { resources = value; }
        }

        public IList<ResourceURIDataContract> ResourcesURI
        {
            get { return resourcesURI; }
            set { resourcesURI = value; }
        }

        public ResourceLoadBalancerServiceClientHelper ServiceClient
        {
            get { return serviceClient; }
            set { serviceClient = value; }
        }

        private void GetFiles(string directory)
        {
 
        }

        public void DeconfigureQueuing(string queueType, string queueName)
        {
            ResourceLoadBalancerServiceClientHelper resourceLoadBalancerServiceClientHelper = new ResourceLoadBalancerServiceClientHelper();
            resourceLoadBalancerServiceClientHelper.DeconfigureQueuing(hostName, queueType, queueName);

        }

        public void SubmitResourceURIs(int batchSize)
        {
            int currentBatchSize = 0;
            int batchCount = 0;

            IList<ResourceURIDataContract> resourceURIBatch = new List<ResourceURIDataContract>();

            currentBatchSize = 0;
            batchCount = 0;

            Console.WriteLine("Processing resources URI");

            for (int i = 0; i < resourcesURI.Count; i++)
            {
                if (currentBatchSize < batchSize)
                {
                    resourceURIBatch.Add(resourcesURI[i]);
                    currentBatchSize++;
                }
                else if (currentBatchSize == batchSize)
                {
                    batchCount++;
                    Console.Write("Sending batch #" + batchCount + "...");
                    serviceClient.AddResourceURIs(resourceURIBatch);
                    Console.Write("Done!\n");
                    currentBatchSize = 0;
                    resourceURIBatch.Clear();
                }
            }
            batchCount++;
            Console.Write("Sending batch #" + batchCount + "...");
            serviceClient.AddResourceURIs(resourceURIBatch);
            Console.Write("Done!\n");
            resourceURIBatch.Clear();
            resourcesURI.Clear();
        }

        public void SubmitResources(int batchSize)
        {
            int currentBatchSize = 0;
            int batchCount = 0;

            IList<ResourceDataContract> resourceBatch = new List<ResourceDataContract>();
            

            Console.WriteLine("Processing resources");

            for (int i = 0; i < resources.Count; i++)
            {
                if (currentBatchSize < batchSize)
                {
                    if (!resources[i].Hash.Equals("Unable to access file"))
                    {
                        resourceBatch.Add(resources[i]);
                        currentBatchSize++;
                    }
                }
                else if (currentBatchSize == batchSize)
                {
                    batchCount++;
                    Console.Write("Sending batch #" + batchCount + "...");
                    
                    try
                    {
                        serviceClient.AddResources(resourceBatch);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                    Console.Write("Done!\n");
                    currentBatchSize = 0;
                    resourceBatch.Clear();
                }
            }
            batchCount++;
            Console.Write("Sending batch #" + batchCount + "...");
            
            try
            {
                serviceClient.AddResources(resourceBatch);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.Write("Done!\n");

            resourceBatch.Clear();
            resources.Clear();
        }
    }


}
